package com.ilovepostcard.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.EditText;

public class Android4StyleEditText extends EditText {

    private Paint mPaint;
    private Rect mRect;
    private float mult = 1.5f;
    private float add = 2.0f;
    private static final int color_normal = Color.GRAY;
    private static final int color_highlight = Color.argb(255, 68, 188, 240);

    public Android4StyleEditText(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public Android4StyleEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public Android4StyleEditText(Context context) {
        super(context);
        init();
    }

    private void init() {
        mRect = new Rect();
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
//        mPaint.setColor(Color.GRAY);
        mPaint.setAntiAlias(true);
        this.setLineSpacing(add, mult);
    }
    
    @Override
    public void onDraw(Canvas canvas) {
        
        mPaint.setColor(isFocused() ? color_highlight : color_normal);
        final int yoffset = 4;
        final int sideHight = 12;
        int count = getLineCount();
        for (int i = 0; i < count; i++) {
            getLineBounds(i, mRect);
            int baseline = (i + 1) * getLineHeight();
            int width = getWidth();
            int baseLineX1 = 0;
            int baseLineX2 = width-2;
            canvas.drawLine(baseLineX1, baseline + yoffset, baseLineX1, baseline + yoffset - sideHight, mPaint);
            canvas.drawLine(baseLineX2, baseline + yoffset, baseLineX2, baseline + yoffset - sideHight, mPaint);
            canvas.drawLine(baseLineX1, baseline + yoffset, baseLineX2, baseline + yoffset, mPaint);
        }
        super.onDraw(canvas);
    }
}
